package com.sihai.core;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.MappedSuperclass;

import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

@MappedSuperclass
public abstract class BaseEntity<ID extends Serializable> implements Serializable, EntityMetaInfo {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 7073484206028533974L;

	public abstract ID getId();

	protected Date create_date;

	protected Long creator_id;

	protected Date modify_date;

	protected Long modifier_id;

	public Date getCreate_date() {
		return this.create_date;
	}

	public void setCreate_date(Date createDate) {
		this.create_date = createDate;
	}

	public Long getCreator_id() {
		return this.creator_id;
	}

	public void setCreator_id(Long creatorId) {
		this.creator_id = creatorId;
	}

	public Date getModify_date() {
		return this.modify_date;
	}

	public void setModify_date(Date modifyDate) {
		this.modify_date = modifyDate;
	}

	public Long getModifier_id() {
		return this.modifier_id;
	}

	public void setModifier_id(Long modifierId) {
		this.modifier_id = modifierId;
	}

	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE, false)
			.concat(String.format("@hashCode=%s", this.hashCode()));
	}

	@Override
	public int hashCode() {
		HashCodeBuilder builder = new HashCodeBuilder(361893073, 878350865);
		if (this.getId() != null) {
			builder.append(this.getId());
		} else {
			builder.append(super.hashCode());
		}
		return builder.hashCode();
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean equals(Object object) {
		if (!BaseEntity.class.isInstance(object)) {
			return false;
		}

		BaseEntity rhs = (BaseEntity) object;
		EqualsBuilder builder = new EqualsBuilder();

		if (this.getId() != null && rhs.getId() != null) {
			builder.append(this.getId(), rhs.getId());
		} else {
			builder.appendSuper(super.equals(object));
		}

		return builder.isEquals();
	}
	
	public String getAbbreviate(String before,int max,String append){
		return WordUtils.abbreviate(before, 0, max, append);
	}
	
	public String getAbbreviate(String before,int max){
		return getAbbreviate(before, max, "...");
	}

}
